#include<ros/ros.h>
#include<tf/transform_broadcaster.h>
#include<tf/transform_listener.h>
#include<geometry_msgs/PointStamped.h>
#include<geometry_msgs/TransformStamped.h>
#include<geometry_msgs/PoseStamped.h>
#include<geometry_msgs/TwistStamped.h>
#include<geometry_msgs/Vector3Stamped.h>
#include<mavros_msgs/ActuatorControl.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include<visualization_msgs/Marker.h>
#include<nav_msgs/Path.h>

//上升结束
int flag_pos=0;
//计数
int count_all=0;
int count_out=0;
//全局变量
mavros_msgs::ActuatorControl input_,output_;
geometry_msgs::PoseStamped pose_out;

//convert_.h函数

geometry_msgs::Vector3Stamped optimization(geometry_msgs::Vector3Stamped input,geometry_msgs::PoseStamped pose_,geometry_msgs::TwistStamped twst);
int testPos(geometry_msgs::PoseStamped pos);



//回调函数
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}

geometry_msgs::PoseStamped current_pose;
void pose_cb(const geometry_msgs::PoseStamped::ConstPtr &msg1)
{
    current_pose=*msg1; 
/*     static tf::TransformBroadcaster br;
    tf::Transform stamped;
    stamped.setOrigin(tf::Vector3(0,0,0));
    tf::Quaternion q(current_pose.pose.orientation.x,current_pose.pose.orientation.y,
    current_pose.pose.orientation.z,current_pose.pose.orientation.w);

    stamped.setRotation(q);
    br.sendTransform(tf::StampedTransform(stamped,ros::Time::now(),"/map","/body"));  */
}

geometry_msgs::TwistStamped current_twist;
void twist_cb(const geometry_msgs::TwistStamped::ConstPtr &msg2)
{
    current_twist= *msg2;

    //ROS_INFO("x=%.2f",current_twist.twist.linear.x);
    //ROS_INFO("y=%.2f",current_twist.twist.linear.y);
    ROS_INFO("z=%.2f",current_twist.twist.linear.z);
}


int main(int argc, char** argv)
{
    ros::init(argc,argv,"ecbf_node");
    ros::NodeHandle nh;

    //tf
    //tf::TransformListener listener;

    //收发器
    ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
            ("mavros/state", 10, state_cb);
    ros::Subscriber  current_vel=nh.subscribe<geometry_msgs::TwistStamped>
            ("/mavros/local_position/velocity_local",10,twist_cb);
    ros::Subscriber local_pos=nh.subscribe<geometry_msgs::PoseStamped>
            ("mavros/local_position/pose",10,&pose_cb);

    ros::Publisher marker_pub=nh.advertise<visualization_msgs::Marker>("marker",10);
    ros::Publisher path_pub = nh.advertise<nav_msgs::Path>("trajectory", 10);
    ros::Publisher point_pub = nh.advertise<geometry_msgs::PointStamped>("point", 10);
    ros::Publisher pos_dst=nh.advertise<geometry_msgs::PoseStamped>
            ("mavros/setpoint_position/local",10); 
    ros::Publisher control_pub=nh.advertise<mavros_msgs::ActuatorControl>
            ("/mavros/actuator_control",10); 
    
    ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
            ("mavros/cmd/arming");
    ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
            ("mavros/set_mode");

    input_.group_mix=3;
    output_.group_mix=3;
    input_.controls[0]=0;
    input_.controls[1]=0;
    input_.controls[2]=5;


    //trajectory
    ros::Time current_time, last_time;
    current_time = ros::Time::now();
    last_time = ros::Time::now();

    nav_msgs::Path path;
    path.header.stamp = current_time;
    path.header.frame_id = "map";


    visualization_msgs::Marker box;
    box.header.frame_id="map";
    box.lifetime=ros::Duration(0);
    box.action=visualization_msgs::Marker::ADD;
    box.ns="cube";
    box.pose.orientation.w=1.0;
    box.id=2;
    box.type=visualization_msgs::Marker::CUBE;
    box.scale.x=3;
    box.scale.y=3;
    box.scale.z=1;
    box.pose.position.x=0;
    box.pose.position.y=0;
    box.pose.position.z=1.5;
    box.color.r = 1.0;
    box.color.a = 0.85;

    
    //边缘
    visualization_msgs::Marker line_list;
    line_list.header.frame_id="map";
    line_list.lifetime=ros::Duration(0);
    line_list.ns="lines";
    line_list.action = visualization_msgs::Marker::ADD;
    line_list.pose.orientation.w = 1.0;
    line_list.id = 2;
    line_list.type = visualization_msgs::Marker::LINE_LIST;
    line_list.scale.x = 0.01;
    line_list.scale.y=0.01;
    line_list.scale.z=0.01;

    // Line list is blue
    line_list.color.b = 1.0;
    line_list.color.a = 1;
    geometry_msgs::Point p1;
    p1.x=-1.5;
    p1.y=-1.5;
    p1.z=1;
    geometry_msgs::Point p2;
    p2.x=1.5;
    p2.y=-1.5;
    p2.z=1;
    geometry_msgs::Point p3;
    p3.x=1.5;
    p3.y=1.5;
    p3.z=1;
    geometry_msgs::Point p4;
    p4.x=-1.5;
    p4.y=1.5;
    p4.z=1;
    geometry_msgs::Point p5;
    p5.x=-1.5;
    p5.y=-1.5;
    p5.z=2;
    geometry_msgs::Point p6;
    p6.x=1.5;
    p6.y=-1.5;
    p6.z=2;
    geometry_msgs::Point p7;
    p7.x=1.5;
    p7.y=1.5;
    p7.z=2;
    geometry_msgs::Point p8;
    p8.x=-1.5;
    p8.y=1.5;
    p8.z=2;

    line_list.points.push_back(p1);
    line_list.points.push_back(p2);
    line_list.points.push_back(p2);
    line_list.points.push_back(p3);
    line_list.points.push_back(p3);
    line_list.points.push_back(p4);
    line_list.points.push_back(p4);
    line_list.points.push_back(p1);

    line_list.points.push_back(p5);
    line_list.points.push_back(p6);
    line_list.points.push_back(p6);
    line_list.points.push_back(p7);
    line_list.points.push_back(p7);
    line_list.points.push_back(p8);
    line_list.points.push_back(p8);
    line_list.points.push_back(p5);

    line_list.points.push_back(p1);
    line_list.points.push_back(p5);
    line_list.points.push_back(p2);
    line_list.points.push_back(p6);
    line_list.points.push_back(p3);
    line_list.points.push_back(p7);
    line_list.points.push_back(p4);
    line_list.points.push_back(p8);

    ros::Rate rate(20.0);

    while(ros::ok() && !current_state.connected)
    {
        ros::spinOnce();
        rate.sleep();
    }
    
    //位置控制
    pose_out.pose.position.x=0;
    pose_out.pose.position.y=0;
    pose_out.pose.position.z=1.5;


    for(int i=100;ros::ok()&&i>0;--i)
    {
        pos_dst.publish(pose_out);
        ros::spinOnce();
        rate.sleep();
    }

    mavros_msgs::SetMode offb_set_mode;
    offb_set_mode.request.custom_mode = "OFFBOARD";
    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;
    ros::Time last_request = ros::Time::now();

    while(ros::ok())
    {
        marker_pub.publish(line_list);
        line_list.header.stamp=ros::Time::now();
        marker_pub.publish(box);
        box.header.stamp=ros::Time::now();

        geometry_msgs::PointStamped this_point_stamped;
        this_point_stamped.header.stamp = current_time;
        this_point_stamped.header.frame_id = "map";
        this_point_stamped.point.x = current_pose.pose.position.x;
        this_point_stamped.point.y = current_pose.pose.position.y;
        this_point_stamped.point.z = current_pose.pose.position.z;

        point_pub.publish(this_point_stamped);

        if(current_state.mode!="OFFBOARD"&&
        (ros::Time::now()-last_request>ros::Duration(5.0)))
        {
            if(set_mode_client.call(offb_set_mode)&&
            offb_set_mode.response.mode_sent)
            {
                ROS_INFO("Offboard enabled");
            }
            last_request=ros::Time::now();
        }
        else
        {
            if(!current_state.armed&&
            (ros::Time::now()-last_request>ros::Duration(5.0)))
            {
                if(arming_client.call(arm_cmd)&&
                arm_cmd.response.success)
                {
                    ROS_INFO("Vehicle armed");
                }
                last_request=ros::Time::now();
            }
        }
        //spinOnce放在两次操作中
        if(flag_pos==0)
        {
            pos_dst.publish(pose_out);
            ros::spinOnce();
            rate.sleep();
            if(current_pose.pose.position.z>1.45)
            {
                flag_pos=1;
            }
        }
        if(flag_pos==1)
        {
            ROS_INFO("KAMEN RIDIER ACCEL!");
/*             geometry_msgs::Vector3Stamped temp;
            temp.header.frame_id="map";
            temp.header.stamp=ros::Time::now(); */
            
            //pose发布

            geometry_msgs::PoseStamped this_pose_stamped;
        
            this_pose_stamped=current_pose;
        
            this_pose_stamped.header.stamp = current_time;
            this_pose_stamped.header.frame_id = "map";
            path.poses.push_back(this_pose_stamped);

            path_pub.publish(path);
        
            last_time = current_time;

            ros::spinOnce();
            rate.sleep();

            int flag=0;
            flag=testPos(current_pose);
            if(flag==3)
            {
                ROS_INFO("within the range");
            }
            if(flag==4)
            {
                ROS_INFO("out of range");
                count_out++;
            }
            count_all++;
            ROS_INFO("detected for %d times, out for %d times",count_all,count_out);
            control_pub.publish(input_);
            

            //output_=optimization(input_,current_pose,current_twist);
/* 
            try
            {
                listener.transformVector("/body",input_,output_);
                ROS_INFO("src=%f,%f,%f, dst=%f,%f,%f",input_.vector.x,input_.vector.y,input_.vector.z,output_.vector.x,output_.vector.y,output_.vector.z);
                
            }
            catch(const std::exception& ex)
            {
                ROS_WARN("%s",ex.what());
                ros::Duration(1.0).sleep();
                continue; 
            } */


        }
    }
    return 0;


}